package LiKouBrushQuestions.SimpleQuestion;

/**
 * 判断回文数
 * @author 23737
 * @time 2021.10.15
 */
public class Test {
    public static void main(String[] args) {
        System.out.println(new SolutionTwo().isPalindrome(-121));
    }
}
//api调用者
class Solution {
    public boolean isPalindrome(int x) {
//        String s = String.valueOf(x);
//        StringBuilder stringBuilder = new StringBuilder();
//        stringBuilder.append(x);
//        StringBuilder reverse = stringBuilder.reverse();
//        return s.equals(reverse.toString());
        return String.valueOf(x).equals(new StringBuilder().append(x).reverse().toString());
    }
}

/**
 * 数学方法
 */
class SolutionTwo{
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        int help = 1;
        int tmp = x;
        while (tmp >= 10) {
            help *= 10;
            tmp /= 10;
        }
        while (x != 0) {
            if (x % 10 != x / help) {
                return false;
            }
            x = x % help / 10;
            help /= 100;
        }
        return true;
    }
}
